package com.example.myapplicationndk.mvp;


import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;


/**
 *生命周期订阅者 订阅 Activity Fragment生命周期
 * rxjava 生命周期管理
 *Author     Vin
 *Mail       vinint@126.com
 *Createtime 2018/7/13 下午3:42
 *Modifytime 2018/7/13 下午3:42
 */
public class RxLifecycleManage implements LifecycleObserver {
    Map<Lifecycle.Event,List<Disposable>> mMapRxLifecycleManage= new HashMap<>();
    /**
     * Constant for onCreate event of the LifecycleOwner.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate(){

    }

    /**
     * Constant for onStart event of the LifecycleOwner
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart(){

    }

    /**
     * Constant for onResume event of the LifecycleOwner
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){

    }

    /**
     * Constant for onPause event of the LifecycleOwner
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        freeDisposable(Lifecycle.Event.ON_PAUSE);
    }
    /**
     * Constant for onStop event of the LifecycleOwner
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        freeDisposable(Lifecycle.Event.ON_STOP);
    }

    /**
     * Constant for onDestroy event of the LifecycleOwner
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy(){
        freeDisposable(Lifecycle.Event.ON_DESTROY);
    }


    /**
     *Method     添加需要释放的Disposable
     *Parameters [disposable]
     *Return     void
     *Author     Vin
     *Mail       vinint@126.com
     *Createtime 2018/7/13 下午4:15
     *Modifytime 2018/7/13 下午4:15
     */
    public void add(Disposable disposable){
        add(disposable, Lifecycle.Event.ON_DESTROY);
    }


    /**
     *Method     添加需要释放的Disposable,根据事件
     *Parameters [disposable, event]
     *Return     void
     *Author     Vin
     *Mail       vinint@126.com
     *Createtime 2018/7/13 下午4:19
     *Modifytime 2018/7/13 下午4:19
     */
    public void add(Disposable disposable, Lifecycle.Event event){
        if ((disposable!=null)&&(!disposable.isDisposed())){
            List<Disposable> disposables;
            if (mMapRxLifecycleManage.containsKey(event)){
                disposables = mMapRxLifecycleManage.get(event);
                disposables.add(disposable);
            }else {
                disposables = new LinkedList<>();
                disposables.add(disposable);
                mMapRxLifecycleManage.put(event,disposables);
            }
        }
    }


    /**
     *Method     释放rxjava资源
     *Parameters [event]
     *Return     void
     *Author     Vin
     *Mail       vinint@126.com
     *Createtime 2018/7/13 下午4:29
     *Modifytime 2018/7/13 下午4:29
     */
    private void freeDisposable(Lifecycle.Event event){
        if (mMapRxLifecycleManage.containsKey(event)){
            List<Disposable> disposables = mMapRxLifecycleManage.get(event);
            for (Disposable d:disposables) {
                if ((d!=null)&&(!d.isDisposed())){
                    d.dispose();
                }
            }
        }
    }

}
